package irv3

import "github.com/astaxie/beego/orm"

type Phy struct {
	Id       int    `orm:"Column(id)"`
	SiteName string `orm:"Column(siteName)"`
	PhyName  string `orm:"Column(phyName)"`
	Type     string `orm:"Column(type)"`
}

type PhyDevice struct {
	SiteName string `orm:"Column(siteName)"`
	DeviceId string `orm:"Column(deviceId)"`
	PhyName  string `orm:"Column(phyName)"`
}

func SelectPhy() (*[]Phy, error) {
	o := orm.NewOrm()
	o.Using("irv3")
	phys := new([]Phy)
	_, err := o.Raw("SELECT site_zone.id AS id, site.display_name AS siteName,site_zone.display_name AS phyName," +
		"site_zone.type FROM site INNER JOIN site_zone ON site.id=site_zone.fk_site_id").QueryRows(phys)
	return phys, err
}

func SelectPhyDevice() (*[]PhyDevice, error) {
	o := orm.NewOrm()
	o.Using("irv3")
	phydevices := new([]PhyDevice)
	_, err := o.Raw("SELECT site.display_name siteName,site_zone.display_name phyName," +
		"device._name AS deviceId FROM site_zone INNER JOIN r_sitezone_devicezone rsd " +
		"ON site_zone.id = rsd.fk_site_zone_id INNER JOIN device_zone ON rsd.fk_device_zone_id=device_zone.id " +
		"INNER JOIN device ON device_zone.fk_device_id=device.id " +
		"INNER JOIN site ON site_zone.fk_site_id = site.id").QueryRows(phydevices)
	return phydevices, err
}
